// components/coupon/index.js
import {
    fetchCouponList
} from '@/services/coupon/index'
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        showCouponPopup: {
            type: Boolean,
            value: false,
            observer(newValue) {
                this.setData({
                    showCouponList: newValue
                })
                if (newValue) {
                    this.getCoupons()
                }
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        selectedCoupon: null,
        showCouponList: false,
        coupons: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getCoupons() {
            const userInfo = JSON.parse(wx.getStorageSync('userInfo') || '{}')
            if (userInfo && userInfo.customerId) {
                fetchCouponList({
                    customerId: userInfo.customerId,
                    isUsed: '否'
                }).then((res) => {
                    this.setData({
                        coupons: res.rows
                    })
                })
            }
        },
        showCouponPopup() {
            this.setData({
                showCouponList: true
            })
        },
        hideCouponPopup() {
            // this.setData({
            //     showCouponList: false
            // })
            this.triggerEvent('close', false);
        },
        // 选择优惠券
        handleCouponChange(e) {
            const {
                item
            } = e.currentTarget.dataset
            this.setData({
                selectedCoupon: item,
                showCouponList: false
            });
            this.triggerEvent('thumb', item);
            this.triggerEvent('close', false);
        },
    }
})